using System;
using System.Collections.Generic;
using System.Linq;
using BepInEx;
using HarmonyLib;
using Hearthstone;
using Hearthstone.Progression;
using PegasusLettuce;
using PegasusShared;
using PegasusUtil;
using UnityEngine;

namespace Mercenary
{
    public static class UtilsInfo
    {
        //showinfo佣兵阵容
        public static int PvpWin = 0;//Pvp胜利次数
        public static int PvpLose = 0;//Pvp失败次数
        private static HashSet<string> MercTeam = new HashSet<string>();//佣兵阵容的集合

        //PVP模式记录对面佣兵的登场情况
        public static void MercRecord(ZonePlay zonePlay_opposing)
        {
            if (GameMgr.Get().GetGameType() == GameType.GT_MERCENARIES_PVP)
            {
                foreach (Card card in zonePlay_opposing.GetCards())
                {
                    if (card.GetEntity().GetTag<TAG_ROLE>(GAME_TAG.LETTUCE_ROLE) != TAG_ROLE.CASTER &&
                        card.GetEntity().GetTag<TAG_ROLE>(GAME_TAG.LETTUCE_ROLE) != TAG_ROLE.TANK &&
                        card.GetEntity().GetTag<TAG_ROLE>(GAME_TAG.LETTUCE_ROLE) != TAG_ROLE.FIGHTER)
                        continue;
                    string equipment = card.GetEntity().GetEquipmentEntity()?.GetName() ?? "";
                    if (equipment.Length > 0 && Char.IsNumber(equipment[equipment.Length - 1]))
                        equipment = equipment.Substring(0, equipment.Length - 1);
                    MercTeam.Add(card.GetEntity().GetName() + '-' + equipment);
                }
            }
        }

        //获取PVP分数
        public static int GetPVPrating()
        {
			if (GameState.Get().GetGameEntity() is LettuceMissionEntity)
			{
				LettuceMissionEntity lettuceGameEntity =
                    (LettuceMissionEntity)GameState.Get().GetGameEntity();
				if (lettuceGameEntity != null && lettuceGameEntity.RatingChangeData != null)
				{
					return lettuceGameEntity.RatingChangeData.PvpRating;
				}
            }
            return 0;
        }

        //佣兵阵容
        public static void MercTeamCount()
        {
            int i = 0;
            string strlog = "";//佣兵阵容
            QueueList<string> allMatchLog = new QueueList<string>();
            if (MercTeam.Count == 0) return;

            //分数
            int rateResult = 0, rateResultDelta = 0;
            if (GameState.Get().GetGameEntity() is LettuceMissionEntity)
            {
                LettuceMissionEntity lettuceGameEntity =
                    (LettuceMissionEntity)GameState.Get().GetGameEntity();
                if (lettuceGameEntity != null && lettuceGameEntity.RatingChangeData != null)
                {
                    rateResult = lettuceGameEntity.RatingChangeData.PvpRating;
                    rateResultDelta = lettuceGameEntity.RatingChangeData.Delta;
                }
            }

            //佣兵阵容
            foreach (string str in MercTeam)
            {
                if (i % 2 == 0) strlog += string.Format("{0}<br/>", str);
                else strlog += string.Format("{0},", str);
                i++;
            }

            //当前胜率 胜-负
            string score = PvpWin.ToString() + '-' + PvpLose.ToString();
            allMatchLog = UtilsLog.ReadFile();

            //分数变化，当前分数，卡组名，佣兵阵容
            UtilsLog.WriteFile(allMatchLog, String.Format("{0},{1},{2},{3},{4}",
                 rateResultDelta.ToString(), rateResult.ToString(),
                 UtilsTeam.m_teamName + ':' + score,
                 (string.IsNullOrEmpty(HsMod.ConfigValue.Get().CacheOpponentFullName))?
                 "官方AI" : HsMod.ConfigValue.Get().CacheOpponentFullName, strlog));
            MercTeam.Clear();
        }

        /*

        //每级经验
        private static readonly List<int> expLevels = new List<int>()
        {
            -1,
            0,100,100,150,150,225,225,300,300,325,
            325,350,350,375,375,400,400,425,425,450,
            450,550,600,650,675,675,875,875,1000,1100,
            1200,1200,1250,1250,1300,1300,1350,1350,1400,1400,
            1450,1450,1500,1500,1550,1550,1600,1600,1650,1650,
            1700,1700,1750,1750,1800,1800,1850,1850,1900,1900,
            1950,1950,2000,2000,2050,2050,2125,2125,2250,2250,
            2375,2375,2500,2500,2500,2500,2500,2500,2500,2500,
            2500,2500,2500,2500,2500,2500,2500,2500,2500,2500,
            2500,2500,2500,2500,2500,2500,2500,2500,2500,2500,

            1325,1325,1325,1325,1325,1350,1350,1350,1350,1375,
            1375,1400,1400,1400,1400,1400,1400,1400,1425,1425,
            1450,1450,1450,1450,1450,1475,1475,1475,1475,1475,
            1500
        };

        private static DateTime dateTimePre = DateTime.Now;//计算经验效率
        private static int totalExp = 0;
        private static bool ExpFlag = false;
        private static string ExpLable = "";//经验效率

        private static double MercXpCalculation()
        {
            
            int PrevXp = 0, CurrXp = 0, PrevLevel = 0, CurrLevel = 0;
            // 经验
            RewardXpNotificationManager rewardXpNotificationManager =
                RewardXpNotificationManager.Get();
            if (rewardXpNotificationManager != null)
            {
                List<RewardTrackXpChange> xpChanges = (List<RewardTrackXpChange>)Traverse.Create(
                    rewardXpNotificationManager).Field("m_xpChanges").GetValue();
                foreach (RewardTrackXpChange xpChange in xpChanges)
                {
                    CurrXp = xpChange.CurrXp;
                    CurrLevel = xpChange.CurrLevel;
                    PrevXp = xpChange.PrevXp;
                    PrevLevel = xpChange.PrevLevel;
                }
            }
            int XpDelta = 0;
            for(int i = PrevLevel; i < CurrLevel; i++)
            {
                if (i <= 131)
                    XpDelta += expLevels[i];
                else XpDelta += 1500;
            }
            XpDelta = XpDelta - PrevXp + CurrXp;
            totalExp += XpDelta;
            TimeSpan TimeDelta = DateTime.Now - dateTimePre;
            UtilsLog.Log(string.Format("[经验演算]开始挂机时间:{0}\t对局结束时间:{1}\t时间间隔:{2}\t",
                dateTimePre, DateTime.Now,TimeDelta.TotalSeconds));
            UtilsLog.Log(string.Format("[经验演算]对局前等级:{0}\t对局后等级{1}\t对局前经验:{2}\t对局后经验:{3}\t对局经验:{4}\t累计总经验:{5}",
                PrevLevel, CurrLevel, PrevXp, CurrXp, XpDelta,totalExp));
            ExpFlag = true;
            return totalExp / TimeDelta.TotalSeconds * 3600;
        }
        */
    }
}